package com.qf.dao.impl;

import com.qf.model.Loans;
import com.qf.model.MyLoans;
import com.qf.model.Navig;
import com.qf.model.PageBean;
import com.qf.model.pojo.Userloans;
import com.qf.utils.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import com.qf.dao.LoansDao;
import org.springframework.jdbc.core.JdbcTemplate;

public class LoansDaoImpl extends  BaseDao implements LoansDao {

    QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
    JdbcTemplate jdbcTemplate =new JdbcTemplate(DruidUtil.getDataSource());

    //查询所有
    @Override
    public List<Loans> findAll() {
        String sql = "select * from loans";
        return  queryForList(Loans.class,sql);
    }
    @Override
    public int addMyLoans(Integer l_id,Integer u_id,Double quota,String c_id){
        String sql="INSERT INTO myloans (l_id,u_id,quota,c_id,type,date) VALUES(?,?,?,?,?,?)";
        Timestamp timestamp = new Timestamp(new Date().getTime());

        int update = jdbcTemplate.update(sql, l_id, u_id, quota, c_id, 0,timestamp);
        return update;
    }

    //管理员  查询所有待审批贷款
    @Override
    public List<Userloans> findAllMyLoans(PageBean<Userloans> pageBean) {

        String sql1="select * from user a inner join myloans b  on a.u_id=b.u_id  inner join loans c  on c.l_id=b.l_id  where  b.type=0 ";
        List<Userloans> list = queryForList(Userloans.class,sql1);
        pageBean.setCount(list.size());
        String sql="select * from user a inner join myloans b  on a.u_id=b.u_id  inner join loans c  on c.l_id=b.l_id  where  b.type=0 limit ?,?";
        int i=(pageBean.getCurPage()-1)*pageBean.getPageSize();
        List<Userloans> userloans = queryForList(Userloans.class, sql, i, pageBean.getPageSize());
        return userloans;
    }
    //管理员拒绝贷款审批

    @Override
    public int sUpdateMyLoans(int l_id) {
        String sql = "update myloans set type=2 where l_id=?";
        return  update(sql,l_id);
    }

    //根据i_id查询贷款信息

    @Override
    public MyLoans findMyLoans(int l_id) {
        String sql = "select * from  myloans where id=?";
        return  queryForOne(MyLoans.class,sql,l_id);
    }
    //根据l_id查询贷款产品信息
    @Override
    public Loans findLoans(int l_id) {
        String sql = "select * from  loans where l_id=?";
        return  queryForOne(Loans.class,sql,l_id);
    }
    //修改贷款信息状态  事务
    @Override
    public int updateMyLoans(Connection conn, int id) {
        try {
            String sql="update myloans set type=1,ready1=0 where id=?";
            int update = queryRunner.update(conn, sql, id);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }


}
